import pygame
import random
import weapon, flame

STARNUM = 80
MAXOBJ = 90
GUNFIRETOP = 130
TOTALFIRETOP = 140

class player_obj(pygame.sprite.Sprite):

    SPARK_INTV = 1
    FIRE_INTV = 2                                           # Fire every 1 frames
    HARM_LAST = 25                                          # How long can player keep in harmless mode
    WEAPON_UPGRADE_LIMIT = 4
    DEFAULT_HP = 10
    DEFAULT_EXP_LIMIT = 500
    BOMB_INTV = 10
    
    def __init__(self, player_num, screen_size, mid = True):
        pygame.sprite.Sprite.__init__(self, self.containers)
        
        self.screen_width = screen_size[0]
        self.screen_height = screen_size[1]
        self.screen_size = screen_size
        self.hp = 10
        self.exp = 0
        self.score = 0
        self.speed = 10
        self.fire_intv = self.FIRE_INTV
        self.player_num = player_num
        self.lives = 3                                      #default player lives is 3
        self.isalive = True
        self.harm_less = self.HARM_LAST                     #How long can player keep in harmless mode
        self.max_exp = self.DEFAULT_EXP_LIMIT               #Weapon level is upgraded by exp
        self.max_hp = self.DEFAULT_HP
        self.weapon_level = 1                               #Default weapon level is 1

        self.flame_spark_intv = self.SPARK_INTV
        self.bomb_intv = self.BOMB_INTV
                
        if (self.player_num == 1):
            #print "player1"
            self.norm_image = pygame.image.load("images/p1.png")
            self.accl_image = pygame.image.load("images/p1accel.png")
            self.image = self.norm_image
            #self._image = self.image
            self.rect = self.image.get_rect()
            
            self.up = pygame.K_w
            self.down = pygame.K_s
            self.left = pygame.K_a
            self.right = pygame.K_d
            self.fire = pygame.K_LCTRL
            self.start = pygame.K_9
            self.bomb = pygame.K_LSHIFT
            
            if (mid):
                self.rect = self.rect.move(self.screen_width / 2 - self.rect.width / 2, self.screen_height - self.rect.height)
            else:
                self.rect = self.rect.move(self.screen_width / 4 - self.rect.width / 2, self.screen_height - self.rect.height)
                
            self.init_pos = self.rect.center
            
        elif (self.player_num == 2):
            self.norm_image = pygame.image.load("images/p2.png")
            self.accl_image = pygame.image.load("images/p2accel.png")
            self.image = self.norm_image
            #self._image = self.image
            self.rect = self.image.get_rect()
            
            self.up = pygame.K_UP
            self.down = pygame.K_DOWN
            self.left = pygame.K_LEFT
            self.right = pygame.K_RIGHT
            self.fire = pygame.K_RCTRL
            self.start = pygame.K_0
            self.bomb = pygame.K_RSHIFT
            
            if (mid):
                self.rect = self.rect.move(self.screen_width / 2 - self.rect.width / 2, self.screen_height - self.rect.height)
            else:
                self.rect = self.rect.move(3 * self.screen_width / 4 - self.rect.width / 2, self.screen_height - self.rect.height)

            self.init_pos = self.rect.center

    def move(self):
        key = pygame.key.get_pressed()
        if (self.isalive):
            if key[self.up]:
                #print "up"
                if (self.rect.top - self.speed >= 0):
                    self.rect = self.rect.move(0,-self.speed)
                flame.flame_spot((self.rect.centerx , self.rect.centery + 20), 0, self.speed, 5, 50)
            if key[self.down]:
                #print "down"
                if (self.rect.bottom + self.speed < self.screen_height):
                    self.rect = self.rect.move(0,self.speed)
                else:
                    self.rect = self.rect.move(0,self.screen_height - self.rect.bottom)
            if key[self.left]:
                #print "left"
                if (self.rect.left - self.speed >= 0):
                    self.rect = self.rect.move(-self.speed,0)
                else:
                    self.rect = self.rect.move(0 - self.rect.left,0)
            if key[self.right]:
                #print "right"
                if (self.rect.right + self.speed < self.screen_width):
                    self.rect = self.rect.move(self.speed,0)
                else:
                    self.rect = self.rect.move(self.screen_width - self.rect.right,0)
            if key[self.fire]:
                if (self.fire_intv == 0):
                    self.fire_laser()
                    self.fire_intv = self.FIRE_INTV
                else:
                    self.fire_intv -= 1
            if key[self.bomb]:
                if (self.fire_intv == 0):
                    self.fire_bomb()
                    self.fire_intv = self.FIRE_INTV
                else:
                    self.fire_intv -= 1
        else:
            if key[self.start]:
                self.recover()
            
        pass

    def is_alived(self):
        return self.isalive

    def is_harm_less(self):
        return self.harm_less

    def reset_pos(self):
        self.harm_less = self.HARM_LAST
        self.rect.center = self.init_pos

    def recover(self):
        if (self.lives):
            self.harm_less
            self.isalive = True
            self.reset_pos()
        else:
            self.kill()

    def destoried(self, oponent): #called by the shot that hit the obj
        self.hp -= oponent.power
        if (self.hp <= 0):
            self.crashed()

    def crashed(self):
        self.isalive = False
        if (self.lives):
            self.lives -= 1
            print "1: ",self.lives
            if (self.weapon_level > 1):
                self.weapon_level -= 1
            flame.explosion(self.rect.center, 50)
            self.recover()
            #flame...

    def draw(self, screen):
        if (self.isalive):                      #display the player if still alive
            if (not (self.harm_less % 2)):      #blink when harmless
                screen.blit(self.image, self.rect)

    def fire_bomb(self):
        if (self.bomb_intv == 0):
            print "bomb!"
            weapon.bomb_obj(self.screen_size, self.rect.center, self, 1)
            self.bomb_intv = self.BOMB_INTV

    def fire_laser(self):
        if (self.weapon_level == 1):
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx - 10,self.rect.centery - 35), self, 1)
            weapon.gun_fire(-10, -15, self)
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx + 10,self.rect.centery - 35), self, 1)
            weapon.gun_fire(10, -15, self)

        elif (self.weapon_level == 2):
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx - 10,self.rect.centery - 35), self, 2)
            weapon.gun_fire(-10, -15, self)
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx + 10,self.rect.centery - 35), self, 2)
            weapon.gun_fire(10, -15, self)


        elif (self.weapon_level == 3):
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx - 10,self.rect.centery - 35), self, 2)
            weapon.gun_fire(-10, -15, self)
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx + 10,self.rect.centery - 35), self, 2)
            weapon.gun_fire(10, -15, self)
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx - 3,self.rect.centery - 38), self, 1)
            weapon.gun_fire(-3, -17, self)
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx + 3,self.rect.centery - 38), self, 1)
            weapon.gun_fire(3, -17, self)
            
        elif (self.weapon_level == 4):
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx - 10,self.rect.centery - 35), self, 2)
            weapon.gun_fire(-10, -15, self)
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx + 10,self.rect.centery - 35), self, 2)
            weapon.gun_fire(10, -15, self)
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx - 3,self.rect.centery - 38), self, 2)
            weapon.gun_fire(-3, -17, self)
            weapon.shot_obj(self.screen_size, 0, -30, (self.rect.centerx + 3,self.rect.centery - 38), self, 2)
            weapon.gun_fire(3, -17, self)
            
        pass

    def weapon_can_up(self):
        return (self.weapon_level < self.WEAPON_UPGRADE_LIMIT)

    def get_hp(self):
        return self.hp

    def get_max_hp(self):
        return self.max_hp

    def get_exp(self):
        return self.exp

    def get_max_exp(self):
        return self.max_exp

    def exp_update(self):
        while (self.exp >= self.max_exp):
            print "UpGrade!"
            if (self.weapon_can_up()):
                self.weapon_level += 1
                self.exp -= self.max_exp
                self.max_exp *= 2         # Here, 2 is a magic number
                
            self.max_hp *= 2                        # Upgrade player's hp limit
            self.hp = self.max_hp

    def update(self):
        if (self.flame_spark_intv == 0):
            self.flame_spark_intv = self.SPARK_INTV
            self.image = self.accl_image
            #self._image = self.image
        else:
            self.flame_spark_intv -= 1
            self.image = self.norm_image
            #self._image = self.image

        if (self.harm_less):
            self.harm_less -= 1

        if (self.bomb_intv):
            self.bomb_intv -= 1
            
        self.move()
